package com.obtk.controllerSW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.model.CstLinkman;
import com.obtk.model.Customer;
import com.obtk.model.SelChance;
import com.obtk.service.CstLinkmanService;
import com.obtk.util.CoreConst;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-04-06 09:38:03
 */
@Controller
@RequestMapping("/cstLinkman")
public class CstLinkmanController {
    @Autowired
    private CstLinkmanService cstLinkmanService;
    @PostMapping("/list")
    @ResponseBody
    public PageResultVo cstLinkman(CstLinkman cstLinkman, Integer limit, Integer offset, Model model){
        System.out.println(cstLinkman);
        PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
        List<CstLinkman> cstLinkmenList = cstLinkmanService.selectCstLinkman(cstLinkman);
        PageInfo<CstLinkman> pages = new PageInfo<>(cstLinkmenList);
        System.out.println(cstLinkmenList);
        return ResultUtil.table(cstLinkmenList,pages.getTotal());
    }
    /**新增用户*/
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(CstLinkman cstLinkmanForm, SelChance selChance, Customer customer){
        String lkmName = cstLinkmanForm.getLkmName();
        //        判断是否电话号相同
        String getLkmMobile = cstLinkmanForm.getLkmMobile();
        CstLinkman cstLinkman = cstLinkmanService.selectByLkmName(lkmName);
        if (null != cstLinkman) {
            return ResultUtil.error("联系人已存在");
        }
        CstLinkman cstLinkman1 = cstLinkmanService.selectByLkmMobile(getLkmMobile);
        if (null != cstLinkman1) {
            return ResultUtil.error("电话已存在");
        }
        int num = cstLinkmanService.register(cstLinkmanForm,selChance,customer);
        if(num > 0){
            return ResultUtil.success("添加联系人成功");
        }else {
            return ResultUtil.error("添加联系人失败");
        }
    }


    /**编辑用户详情*/
    @GetMapping("/edit")
    public String userDetail(Model model, Integer id){
        CstLinkman cstLinkman = cstLinkmanService.selectByLkmId(id);
        System.out.println("controller"+id);
        model.addAttribute("cstLinkman", cstLinkman);
        return "cstLinkman/update";
    }

    /**编辑用户*/
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(CstLinkman cstLinkmanForm){
        int a = cstLinkmanService.updateByCstLinkmanId(cstLinkmanForm);
        if (a > 0) {
            return ResultUtil.success("编辑联系人成功！");
        } else {
            return ResultUtil.error("编辑联系人失败");
        }
    }
    /**删除商品*/
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String id) {
        List<String> lkmIdsList = Arrays.asList(id);
        int a = cstLinkmanService.updateStatusBatch(lkmIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除联系人成功");
        } else {
            return ResultUtil.error("删除联系人失败");
        }
    }
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String idsStr) {
        String[] ids = idsStr.split(",");
        List<String> cstOrderIdsList = Arrays.asList(ids);
        int a = cstLinkmanService.updateStatusBatch(cstOrderIdsList,CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除联系人成功");
        } else {
            return ResultUtil.error("删除联系人失败");
        }
    }
}
